package com.mirror.llk.view;

import android.content.Context;
import android.content.Intent;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Delay;
import com.badlogic.gdx.scenes.scene2d.actions.MoveTo;
import com.badlogic.gdx.scenes.scene2d.actions.Parallel;
import com.badlogic.gdx.scenes.scene2d.interpolators.DecelerateInterpolator;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
import com.mirror.llk.ResumeChooseActivity;
import com.mirror.llk.ThemeChooseActivity;
import com.mirror.llk.WelActivity;

/**
 * 首页视图.
 * <p>
 * Copyright: Copyright (c) 2013-7-8 下午3:24:34
 * <p>
 * Company: DIY工作室
 * <p>
 * Author: 李晖
 * <p>
 * Version: 1.0
 * <p>
 */
public class MainView implements ApplicationListener {

	/** 开始、继续、比分等按钮 */
	private Button[] button = new Button[4];

	/** 首页上下文，由构造函数传入 */
	private Context context;

	/** 舞台 */
	private Stage stage;

	/** 按钮的二态图形 */
	private Texture normalTexture, pressTexture;

	/** 按钮名称 */
	private String[] buttonName = { "start", "resume", "score", "game1" };

	/** 按钮文本 */
	private String[] buttonText = { "New Game", "Resume", "Score", "Game1" };

	public MainView(Context context) {
		this.context = context;
	}

	@Override
	public void create() {
		stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
		normalTexture = new Texture(Gdx.files.internal("mainNormal.png"));
		pressTexture = new Texture(Gdx.files.internal("mainPress.png"));
		NinePatch normal = new NinePatch(normalTexture, 7, 7, 9, 9);
		NinePatch press = new NinePatch(pressTexture, 7, 7, 9, 9);
		BitmapFont bitmapFont = new BitmapFont(Gdx.files.internal("cf.fnt"), Gdx.files.internal("cf.png"), false);
		float duration = 1f;
		ButtonStyle buttonStyle = new ButtonStyle(normal, press, press, 0f, 0f, 0f, 0f, bitmapFont, new Color(1, 1, 0, 0.5f));
		for (int i = 0; i < button.length; i++) {
			button[i] = new Button(buttonText[i], buttonStyle, buttonName[i]);
			button[i].width = 200f;
			button[i].height = 60f;
			button[i].x = (Gdx.graphics.getWidth() - button[i].width) / 2;
			button[i].y = Gdx.graphics.getHeight() + (button.length - 1 - i) * 80;
			// 移动
			MoveTo moveTo = MoveTo.$((Gdx.graphics.getWidth() - button[i].width) / 2,
			                         (Gdx.graphics.getHeight() - button[i].height) / 2 - i * 80 - 10, duration);
			// 减速移动，减速度为1
			moveTo.setInterpolator(DecelerateInterpolator.$(2));
			// 添加事件组合
			button[i].action(Parallel.$(Delay.$(moveTo, duration / 2)));
		}

		button[0].setClickListener(new ClickListener() {

			@Override
			public void click(Actor arg0) {
				Intent intent = new Intent(context, ThemeChooseActivity.class);
				context.startActivity(intent);
			}
		});

		button[1].setClickListener(new ClickListener() {

			@Override
			public void click(Actor arg0) {
				Intent intent = new Intent(context, ResumeChooseActivity.class);
				context.startActivity(intent);
			}
		});

		button[2].setClickListener(new ClickListener() {

			@Override
			public void click(Actor arg0) {
			}
		});

		button[3].setClickListener(new ClickListener() {

			@Override
			public void click(Actor arg0) {
				Intent intent = new Intent(context, WelActivity.class);
				context.startActivity(intent);
			}
		});
		for (Button btn : button) {
			stage.addActor(btn);
		}
		Gdx.input.setInputProcessor(stage);
		Music music = Gdx.audio.newMusic(Gdx.files.internal("ref_pull_back.ogg"));
		music.setLooping(false);
		music.setVolume(15f);
		music.play();
	}

	@Override
	public void dispose() {
		normalTexture.dispose();
		pressTexture.dispose();
		stage.dispose();
	}

	@Override
	public void pause() {
		dispose();
	}

	@Override
	public void render() {
		Gdx.gl.glClearColor(1, 1, 1, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		stage.act(Gdx.graphics.getDeltaTime());
		stage.draw();
	}

	@Override
	public void resize(int arg0, int arg1) {

	}

	@Override
	public void resume() {
		create();
	}

}
